package com.fancy.learncenter.view;

import android.support.v7.widget.GridLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.support.v7.widget.helper.ItemTouchHelper;

import com.fancy.learncenter.adapter.base.CommonRecycleViewAdapter;
import com.fancy.learncenter.utils.LogUtil;

import java.util.Collections;

public class MyItemTouchCallback extends ItemTouchHelper.Callback {

    private final CommonRecycleViewAdapter adapter;

    public MyItemTouchCallback(CommonRecycleViewAdapter adapter) {
        this.adapter = adapter;
    }

    @Override
    public int getMovementFlags(RecyclerView recyclerView, RecyclerView.ViewHolder viewHolder) {
        int dragFlag;
        int swipeFlag;
        RecyclerView.LayoutManager layoutManager = recyclerView.getLayoutManager();
        if (layoutManager instanceof GridLayoutManager) {
//            dragFlag = ItemTouchHelper.DOWN | ItemTouchHelper.UP;
            dragFlag = ItemTouchHelper.DOWN | ItemTouchHelper.UP
                    | ItemTouchHelper.RIGHT | ItemTouchHelper.LEFT;
            swipeFlag = 0;
        } else {
            dragFlag = ItemTouchHelper.DOWN | ItemTouchHelper.UP;
            if (isSwipeDelete) {
                swipeFlag = ItemTouchHelper.END;
            } else {
                swipeFlag = 0;
            }


        }
        return makeMovementFlags(dragFlag, swipeFlag);
    }

    private boolean isSwipeDelete = false;

    /**
     * 是否允许滑动删除（false，不滑动删除）
     *
     * @param isSwipeDelete
     */
    public void setSwipeDelete(boolean isSwipeDelete) {
        this.isSwipeDelete = isSwipeDelete;
    }

    @Override
    public boolean onMove(RecyclerView recyclerView, RecyclerView.ViewHolder viewHolder,
                          RecyclerView.ViewHolder target) {

        int fromPosition = viewHolder.getAdapterPosition();

        int toPosition = target.getAdapterPosition();

        if (toPosition >= adapter.itemDatas.size()) {
            toPosition = adapter.itemDatas.size() - 1;
        }
        LogUtil.MyLog("toPosition", fromPosition + "=====toPosition====" + toPosition + "--------" + adapter.itemDatas.size());
        if (fromPosition < toPosition) {
            for (int i = fromPosition; i < toPosition; i++) {

                if (i + 1 > adapter.itemDatas.size() - 1) {

                } else {
                    Collections.swap(adapter.itemDatas, i, i + 1);
                }

            }
        } else {
            for (int i = fromPosition; i > toPosition; i--) {
                Collections.swap(adapter.itemDatas, i, i - 1);
            }
        }
        recyclerView.getAdapter().notifyItemMoved(fromPosition, toPosition);
        return true;
    }

    @Override
    public void onSwiped(RecyclerView.ViewHolder viewHolder, int direction) {
        LogUtil.MyLog("MyItemTouchCallback", "=====onSwiped===" + direction);
        int position = viewHolder.getAdapterPosition();
        if (direction == ItemTouchHelper.END) {
            adapter.itemDatas.remove(position);
            adapter.notifyItemRemoved(position);
        }
    }

    @Override
    public void onSelectedChanged(RecyclerView.ViewHolder viewHolder, int actionState) {
        LogUtil.MyLog("MyItemTouchCallback", "=====onSelectedChanged===" + actionState);
        super.onSelectedChanged(viewHolder, actionState);
        if (actionState == ItemTouchHelper.ACTION_STATE_DRAG) {
            viewHolder.itemView.setAlpha(0.6f);
        }

        if (swipeCallBack != null) {
            swipeCallBack.startSwipe();
        }
    }

    @Override
    public void clearView(RecyclerView recyclerView, RecyclerView.ViewHolder viewHolder) {
        super.clearView(recyclerView, viewHolder);
        viewHolder.itemView.setAlpha(1f);


        if (swipeCallBack != null) {
            swipeCallBack.endSwipe();
        }
    }

    private SwipeCallBack swipeCallBack;

    public SwipeCallBack getSwipeCallBack() {
        return swipeCallBack;
    }

    public void setSwipeCallBack(SwipeCallBack swipeCallBack) {
        this.swipeCallBack = swipeCallBack;
    }

    public interface SwipeCallBack {
        abstract void startSwipe();

        abstract void endSwipe();
    }
}
